(function (){

	const host = 'http://127.0.0.1';
    const vm = new Vue({
        el: '#app',
        data() {
            return {
                urlHash:['recommend','new','douyin','search'],
                dialogVisible:true,
                aplayer:{},
                clientHeight:'',
                activeIndex: '1',
                keyword:'',
                hotkeys:{},
                singerIntro:{},
                hasSingerIntro:false,
                showSearchPage:false,
                searchSongs:[],
                peakKtvSongs:[],
                peakNewSongs:[],
                peakDouyinSongs:[],
                playList:[{
                    songid:"",
                    songmid:'',
                    name:'',
                    artist:'',
                    cover:'',
                    url:'',
                    lrc:"暂无歌词"
                }],
                curSongIndex:0,
                loading:false,
                firstLoading:{
                    peakKtv:false,
                    peakNew:false,
                    search:false,
                    peakDouyin:false
                },
                pages:1,
                currentPage:1,
                pageSize:20,
                music:{
                    isPlay:false,
                    currentTime:0,
                    maxTime:0,
                    volume:100
                },
                err:0
            };
        },
        methods: {
            browserChangeFixed(clientHeight){ //动态修改样式
                this.$refs.homePage.$el.style.height = clientHeight-20+'px';
            },
            handleSelect(key) {
                window.location.hash = this.urlHash[parseInt(key)-1];
                vm.$refs.scrollDom.$el.scrollTop = 0;
                this.activeIndex = key;
            },
            search(){
                this.showSearchPage = true;
                vm.firstLoading.search = true;
                axios.get(host+'/music/search',{
                    params: {
                        keyword: vm.keyword,
                        page:1,
                        num:vm.pageSize
                    }
                }).then(function (response) {
                    // console.log(response);
                    response = JSON.parse(response.data.result);
                    // console.log("search=",response.data.song);
                    vm.currentPage = response.data.song.curpage;
                    vm.pages = Math.ceil(parseInt(response.data.song.totalnum)/vm.pageSize);

                    // console.log("cur="+vm.currentPage,vm.pages);
                    // console.log("loading="+vm.loading,"disabled="+vm.disabled());
                    vm.searchSongs = response.data.song.list;
                    // console.log(vm.songs.length);
                    if (response.data.zhida.type=='1') {
                        vm.hasSingerIntro = true;
                        // console.log(vm.hasSingerIntro);
                        vm.singerIntro = response.data.zhida.zhida_singer;
                    }else{
                        vm.hasSingerIntro = false;
                    }
                    vm.firstLoading.search = false;
                }).catch(function (error) {
                    vm.$message.error('网络错误');
                });
            },
            load(){
                if (this.currentPage<this.pages) {
                    this.loading = true;
                    axios.get(host+'/music/search',{
                        params: {
                            keyword: vm.keyword,
                            page:vm.currentPage+1,
                            num:vm.pageSize
                        }
                    }).then(function (response) {
                        // console.log(response);
                        response = JSON.parse(response.data.result);
                        // console.log("load=",response.data.song);
                        vm.currentPage = response.data.song.curpage;
                        // console.log("cur="+vm.currentPage,vm.pages);
                        response.data.song.list.forEach(function (item) {
                            vm.searchSongs.push(item);
                        });
                        vm.loading = false;

                    }).catch(function (error) {
                        vm.$message.error('网络错误');
                    });
                }
            },
            getScroll(e) {

                // this.scrollTop[this.activeIndex-1] = e.target.scrollTop;

                if (this.activeIndex==4&&this.showSearchPage) {
                    // 滚动条距离底部的距离scrollBottom
                    let scrollBottom =e.target.scrollHeight - e.target.scrollTop - e.target.clientHeight;
                    if (scrollBottom < 5) {
                        e.target.scrollTop-=50;
                        this.load();
                    }
                }
            },
            getHotkey(){
                axios.get(host+'/music/hotkey').then(function (response) {
                    vm.hotkeys = JSON.parse(response.data.result).data;
                    // console.log(response.data.result)
                }).catch(function (error) {
                    vm.$message.error('网络错误');
                });
            },
            handleClose(done){
                this.hasSingerIntro = false;
                done();
            },
            hotkeySearch(hotkey){
                this.keyword = hotkey;
                this.search();
            },
            closeSearchPage(){
                this.showSearchPage = false;
                this.searchSongs = [];
            },
            peakKtv(){
                this.firstLoading.peakKtv = true;
                axios.get(host+'/music/peak-ktv').then(function (response) {
                    response = JSON.parse(response.data.result);
                    vm.peakKtvSongs = response.songlist;
                    vm.firstLoading.peakKtv = false;
                    // console.log(vm.peakKtvSongs);
                }).catch(function (error) {
                    vm.$message.error('网络错误');
                });
            },
            peakNew(){
                this.firstLoading.peakNew = true;
                axios.get(host+'/music/peak-new').then(function (response) {
                    response = JSON.parse(response.data.result);
                    vm.peakNewSongs = response.songlist;
                    vm.firstLoading.peakNew = false;
                    // console.log(vm.peakKtvSongs);
                }).catch(function (error) {
                    vm.$message.error('网络错误');
                });
            },
            peakDouyin(){
                this.firstLoading.peakDouyin = true;
                axios.get(host+'/music/peak-douyin').then(function (response) {
                    // console.log(response);
                    let data = response.data;
                    // if(data.detail==undefined){
                    //     vm.err++;
                    //     if (vm.err<=5){
                    //         vm.peakDouyin();
                    //     }
                    // }else {
                    try {
                        vm.peakDouyinSongs = data.detail.data.songInfoList;
                    }catch (e) {}
                    vm.firstLoading.peakDouyin = false;
                        // console.log(vm.peakKtvSongs);
                    // }
                }).catch(function (error) {
                    vm.$message.error('网络错误');
                    // console.log('dy');
                });
            },
            playSong(row){
                // let flag = true;//peak
                // try{
                //     row.data.songmid;
                // }catch (e) {//search
                //     flag = false
                // }
                // if (flag){//peak
                //     row = row.data;
                // }

                if (this.activeIndex==1||this.activeIndex==2){
                    row = row.data;
                }else if (this.activeIndex==3) {
                    row.songmid = row.mid;
                    row.songname = row.name;
                    row.albummid = row.album.mid;
                    row.songid = row.id;
                }

                for (var i = 0; i < vm.playList.length; i++) {
                    if (vm.playList[i].songmid==row.songmid){
                        if (vm.playList[i].url==''){
                            vm.$message('歌曲无版权');
                        }else {
                            vm.curSongIndex = i;
                            this.aplayer.list.switch(i-1);
                            this.aplayer.play();
                        }
                        return;
                    }
                }
                axios.get(host+'/music/song/'+row.songmid).then(function (response) {
                    // console.log(row.songname);
                    if (response.data.result==''){
                        vm.$message('歌曲无版权');
                        // console.log(123);
                        return;
                    }else {
                        vm.curSongIndex = vm.playList.length;
                    }
                    let singers = "";
                    row.singer.forEach(function (item) {
                        // console.log(item.name);
                        singers = singers+item.name+"/";
                    });
                    singers = singers.substring(0,singers.length-1);
                    vm.playList.push({
                        songmid : row.songmid,
                        name : row.songname,
                        url : response.data.result,
                        cover : 'https://y.gtimg.cn/music/photo_new/T002R300x300M000'+row.albummid+'.jpg',
                        artist : singers,
                        songid:row.songid
                    });
                    setTimeout(function () {
                        try{
                            vm.aplayer.list.add([vm.playList[vm.curSongIndex]]);

                            if (vm.curSongIndex>1){
                                // vm.aplayer.skipForward();
                                vm.aplayer.list.switch(vm.aplayer.list.audios.length-1);
                            }
                            vm.aplayer.play();
                        }catch (e) {
                        }
                    },300);
                    vm.getLyric();
                }).catch(function (error) {
                    vm.$message.error('网络错误');
                });
            },
            asciiChange(lyric){

                lyric = lyric.replace(/&#13;/g,'\n');
                lyric = lyric.replace(/&#10;/g,'\r');

                lyric = lyric.replace(/&#32;/g,' ');
                lyric = lyric.replace(/&#33;/g,'!');
                lyric = lyric.replace(/&#34;/g,'"');
                lyric = lyric.replace(/&#36;/g,'$');
                lyric = lyric.replace(/&#37;/g,'%');
                lyric = lyric.replace(/&#39;/g,"'");
                lyric = lyric.replace(/&#40;/g,'(');
                lyric = lyric.replace(/&#41;/g,')');
                lyric = lyric.replace(/&#42;/g,'*');
                lyric = lyric.replace(/&#43;/g,'+');
                lyric = lyric.replace(/&#44;/g,',');
                lyric = lyric.replace(/&#45;/g,'-');
                lyric = lyric.replace(/&#46;/g,'.');
                lyric = lyric.replace(/&#47;/g,'/');
                lyric = lyric.replace(/&#58;/g,':');
                lyric = lyric.replace(/&#60;/g,'<');
                lyric = lyric.replace(/&#61;/g,'=');
                lyric = lyric.replace(/&#62;/g,'>');
                lyric = lyric.replace(/&#63;/g,'?');
                lyric = lyric.replace(/&#64;/g,'@');
                lyric = lyric.replace(/&#92;/g,'\\');

                lyric = lyric.replace(/&#94;/g,'^');
                lyric = lyric.replace(/&#95;/g,'_');
                lyric = lyric.replace(/&#96;/g,'`');
                lyric = lyric.replace(/&#123;/g,'{');
                lyric = lyric.replace(/&#124;/g,'|');
                lyric = lyric.replace(/&#125;/g,'}');
                lyric = lyric.replace(/&#126;/g,'~');


                lyric = lyric.replace(/&#35;/g,'#');
                lyric = lyric.replace(/&#38;/g,'&');
                lyric = lyric.replace(/&#91;/g,'[');
                lyric = lyric.replace(/&#93;/g,']');
                lyric = lyric.replace(/&#59;/g,';');
                return lyric;
            },
            getLyric(){
                let songmid = vm.playList[vm.curSongIndex].songmid;
                let songid = vm.playList[vm.curSongIndex].songid;
                if (songmid==''||songid==''){
                    return;
                }
                axios.get(host+'/music/lyric',{
                    params:{
                        songid:songid,
                        songmid:songmid
                    }
                }).then(function (response) {

                    let lyric = JSON.parse(response.data.result).lyric;

                    if (lyric==undefined){
                        return;
                    }

                    vm.playList[vm.curSongIndex].lrc = vm.asciiChange(lyric);

                    // console.log(JSON.stringify(vm.playList));
                    // console.log(vm.curSongIndex);
                }).catch(function (error) {
                    // console.log(222)
                });
            }
        },
        created(){
            let urlHash = window.location.hash;
            switch (urlHash) {
                case '#new':
                    this.activeIndex = '2';;break;
                case '#douyin':
                    this.activeIndex = '3';break;
                case '#search':
                    this.activeIndex = '4';break;
                default :
                    this.activeIndex = '1';break;
            }
            this.dialogVisible = (this.activeIndex == '1');
            this.getHotkey();
            this.peakKtv();
            this.peakNew();
            this.peakDouyin();
        },
        mounted(){
            // 获取浏览器可视区域高度
            this.clientHeight =   `${document.documentElement.clientHeight}`;

            window.onresize = function temp() {
                this.clientHeight = `${document.documentElement.clientHeight}`;
            };

            this.aplayer = new APlayer({
                container: document.getElementById('aplayer'),
                lrcType: 1,
                fixed: true,
                autoplay:true,
                listFolded: true,
                listMaxHeight: 90,
                audio: []
            });
            this.aplayer.disableVersionBadge = true;

            this.$nextTick(function () {
                vm.$refs.scrollDom.$el.addEventListener('scroll',vm.getScroll);
            });
        },
        watch: {
            // 如果 `clientHeight` 发生改变，这个函数就会运行
            clientHeight: function () {
                this.browserChangeFixed(this.clientHeight)
            },
        }
    });
})();


